/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.gg.reader.api.protocol.gx.LogBaseEpcInfo;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springblade.common.enums.item.ItemOperateType;
import org.springblade.common.exception.TrainingServiceException;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.modules.item.entity.ItemEntity;
import org.springblade.modules.item.entity.ItemStockLogEntity;
import org.springblade.modules.item.service.IStockLogService;
import org.springblade.modules.item.vo.ItemVO;
import org.springblade.modules.item.mapper.ItemMapper;
import org.springblade.modules.item.service.IItemService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;


import java.util.Date;
import java.util.List;
import java.util.Map;

import static org.springblade.common.constant.RedisKeyConstant.RFID_KEY_PREFIX;
import static org.springblade.modules.item.service.impl.StockLogServiceImpl.getStockLog;

/**
 * 教具 服务实现类
 *
 * @author junxin
 * @since 2023-03-22
 */
@AllArgsConstructor
@Service
public class ItemServiceImpl extends BaseServiceImpl<ItemMapper, ItemEntity> implements IItemService {

	private final IStockLogService stockLogService;
	private final BladeRedis bladeRedis;

	@Override
	public IPage<ItemVO> selectItemPage(IPage<ItemVO> page, ItemVO item) {
		return page.setRecords(baseMapper.selectItemPage(page, item));
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean stockChange(ItemVO vo) {
		ItemStockLogEntity logEntity=getStockLog(vo);
		ItemEntity entity = baseMapper.selectById(vo.getItemId());
		if (vo.getType().equals(ItemOperateType.WAREHOUSE.getCode())){ //入库操作
			entity.setStock(entity.getStock()+vo.getNumber()); //增加库存数量
			entity.setTotal(entity.getTotal()+ vo.getNumber()); //增加库存总数
			logEntity.setType(ItemOperateType.WAREHOUSE.getCode().toString());
			logEntity.setDeliveryTime(new Date());
		}

		if (vo.getType().equals(ItemOperateType.OUTSTOCK.getCode())){ //出库操作
			if (entity.getStock()<vo.getNumber() || entity.getTotal()< vo.getNumber() ) 	throw new ServiceException("库存不足,出库失败!");
			entity.setStock(entity.getStock()-vo.getNumber());  //减少库存数量
			entity.setTotal(entity.getTotal()-vo.getNumber()); //减少库存总数
			logEntity.setType(ItemOperateType.OUTSTOCK.getCode().toString());
			logEntity.setWarehousingTime(new Date());
		}

		if (vo.getType().equals(ItemOperateType.RETURN.getCode())){ //归还操作
			entity.setStock(entity.getStock()+vo.getNumber());  //增加库存数量
			logEntity.setStatus(1);//表示已归还
			logEntity.setDeliveryTime(new Date());
			logEntity.setType(ItemOperateType.RETURN.getCode().toString());
		}

		if (vo.getType().equals(ItemOperateType.BORROW.getCode())){ //借出操作
			if (entity.getStock()<vo.getNumber()) 	throw new ServiceException("库存不足,借出失败!");
			entity.setStock(entity.getStock()-vo.getNumber());  //减少库存数量
			logEntity.setStatus(2);//表示待归还
			logEntity.setWarehousingTime(new Date());
			logEntity.setType(ItemOperateType.BORROW.getCode().toString());
			Map<String, LogBaseEpcInfo> map = inventoryItem();
			if (CollectionUtil.isNotEmpty(map)) {
				map.remove(entity.getTid());
			}
		}

		if (vo.getType().equals(ItemOperateType.DESTORY.getCode())){ //销毁操作
			if (entity.getTotal()<vo.getNumber()) 	throw new ServiceException("库存不足,销毁失败!");
			entity.setStock(entity.getStock()-vo.getNumber());  //减少库存数量
			entity.setTotal(entity.getTotal()-vo.getNumber()); //减少库存总数
			logEntity.setType(ItemOperateType.DESTORY.getCode().toString());
		}
		logEntity.setNumber(vo.getNumber());
		logEntity.setItemId(vo.getItemId());
		logEntity.setOperationBackup(vo.getOperationBackup());
		if (StringUtils.isBlank(logEntity.getType())) throw new TrainingServiceException("暂时无法进行此操作");
		stockLogService.save(logEntity);
		return baseMapper.updateById(entity)>0;
	}

	@Override
	public List<ItemEntity> queyByWrapper(Wrapper<ItemEntity> queryWrapper) {
		return baseMapper.selectList(queryWrapper);
	}

	@Override
	public Map<String, LogBaseEpcInfo> inventoryItem() {
		return bladeRedis.get(RFID_KEY_PREFIX);
	}



}
